<?php

require_once 'cms/core/om/om/BaseImage.php';


/**
 * Skeleton subclass for representing a row from the 'image' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package core.om
 */	
class Image extends BaseImage {


    /**
     * Get the associated BinaryFile object
     *
     * @param Connection Optional Connection object.
     * @return BinaryFile The associated BinaryFile object.
     * @throws PropelException
     */
    public function getBinaryFile($con = null)
    {
        // include the related Peer class
        include_once 'cms/core/om/om/BaseBinaryFilePeer.php';

        if ($this->aBinaryFile === null && ($this->binary_file_id !== null)) {

            $c = new Criteria();
            $c->clearSelectColumns();
            $c->addSelectColumn(BinaryFilePeer::BINARY_FILE_ID);
            $c->addSelectColumn(BinaryFilePeer::FILE_NAME);
            $c->addSelectColumn(BinaryFilePeer::FILE_SIZE);
            $c->addSelectColumn(BinaryFilePeer::CONTENT_TYPE);
            $c->add(BinaryFilePeer::BINARY_FILE_ID, $this->binary_file_id);

            $rs = BinaryFilePeer::doSelectRS($c, $con);
            while($rs->next()) {
                $this->aBinaryFile = new BinaryFile();
                $this->aBinaryFile->setBinaryFileId($this->binary_file_id);
                $this->aBinaryFile->setFileName($rs->getString(2));
                $this->aBinaryFile->setFileSize($rs->getInt(3));
                $this->aBinaryFile->setContentType($rs->getString(3));
            }

        }
        return $this->aBinaryFile;
    }


    /**
     * Save an image posted by a form.
     *
     * @param formFile $_FILES entry from a form.
     */
    public function saveForm(&$formFile, $conn = null) {

        $log =& LoggerManager::getLogger("Image");

        require_once("cms/core/om/BinaryFile.php");

        // Save the binaryFile first.
        // To update post image->binaryFileId
        $binaryFile = new BinaryFile();
        $binaryFile->setBinaryFileId($this->getBinaryFileId());
        $binaryFile->saveForm($formFile, $conn);

        $size = getimagesize($formFile["tmp_name"]);
        $this->setWidth($size[0]);
        $this->setHeight($size[1]);
        $this->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));
        $this->setBinaryFileId($binaryFile->getBinaryFileId());

        if($this->getImageId() > 0) {

            ImagePeer::doUpdate($this, $conn);

        } else {

            $this->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
            $this->setImageId(
                ImagePeer::doInsert($this, $conn)
            );

        }

    }


    /**
     * Save an image using the filesystem.
     *
     * @param realfile The full path to the file
     * @param filename The new filename filename
     * @param conn The database connection to use.
     */
    public function saveFile($realfile, $filename=null, $conn = null) {

        require_once("cms/core/om/BinaryFile.php");

        $log =& LoggerManager::getLogger("Image");

        $size = getimagesize($realfile);

        $type = "";
        switch($size[2]) {
            case 1:
                $type = "image/gif";
                break;

            case 2:
                $type = "image/jpeg";
                break;

            case 3:
                $type = "image/png";
                break;
        }

        // Save the binaryFile first.
        // To update post image->binaryFileId
        $binaryFile = new BinaryFile();
        $binaryFile->setBinaryFileId($this->getBinaryFileId());
        $binaryFile->setContentType($type);
        $binaryFile->saveFile($realfile, $filename, $conn);

        $this->setWidth($size[0]);
        $this->setHeight($size[1]);
        $this->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));
        $this->setBinaryFileId($binaryFile->getBinaryFileId());

        if($this->getImageId() > 0) {

            ImagePeer::doUpdate($this, $conn);

        } else {

            $this->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
            $this->setImageId(
                ImagePeer::doInsert($this, $conn)
            );

        }

    }

} // Image
